package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

import model.Car;
import model.CarModel;
import model.Driver;
import model.LicensePlate;
import view.ServerViewPanelCar;
import databaseController.DatabaseController;

public class ServerCarController {

	private ServerViewPanelCar panel;
	
	public ServerCarController() {
		this.panel = new ServerViewPanelCar();
		this.panel.getjCheckBoxStolen().setSelected(false);
		this.panel.getjComboBoxCarModel().setSelectedItem(null);
		this.panel.getjComboBoxDriver().setSelectedItem(null);		
		
		this.panel.getjComboBoxCarModel().setModel(new DefaultComboBoxModel(DatabaseController.getInstance().getCarModelsDatabase().toArray()));
		this.panel.getjComboBoxDriver().setModel(new DefaultComboBoxModel(DatabaseController.getInstance().getDriversDatabase().toArray()));
		this.panel.getjComboBoxType().setModel(new DefaultComboBoxModel(new String[] {"Normal", "Verde", "Vermelho", "Amarelo", "Preto"}));
		
		this.panel.getjButtonConfirm().addActionListener(new ListenerConfirm());
		this.panel.getjButtonClear().addActionListener(new ListenerClear());
	}
	
	public ServerViewPanelCar getPanel() {
		return this.panel;
	}
	
	private void clear() {
		ServerCarController.this.panel.getjTextFieldLicensePlate().setText("");
		ServerCarController.this.panel.getjTextFieldUf().setText("");
		ServerCarController.this.panel.getjTextFieldCity().setText("");
		ServerCarController.this.panel.getjTextFieldImage().setText("");
		
		ServerCarController.this.panel.getjComboBoxCarModel().setSelectedItem(null);
		ServerCarController.this.panel.getjComboBoxDriver().setSelectedItem(null);
		ServerCarController.this.panel.getjCheckBoxStolen().setSelected(false);		
	}
	
	class ListenerConfirm implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Colocar registro de motorista na base de dados
			String licensePlate = ServerCarController.this.panel.getjTextFieldLicensePlate().getText();
			String uf = ServerCarController.this.panel.getjTextFieldUf().getText();
			String city = ServerCarController.this.panel.getjTextFieldCity().getText();
			String image = ServerCarController.this.panel.getjTextFieldImage().getText();
			
			CarModel carModel = (CarModel) ServerCarController.this.panel.getjComboBoxCarModel().getSelectedItem();
			Driver driver = (Driver) ServerCarController.this.panel.getjComboBoxDriver().getSelectedItem();
			String type = (String) ServerCarController.this.panel.getjComboBoxType().getSelectedItem();
			
			boolean stolen = ServerCarController.this.panel.getjCheckBoxStolen().isSelected();
			
			
			DatabaseController.getInstance().addCar(new Car(new LicensePlate(type, licensePlate, city, uf), carModel, driver, image, stolen));
			
			JOptionPane.showMessageDialog(null,
				    "Carro cadastrado com sucesso!\n " , 
				    "Sucesso", JOptionPane.INFORMATION_MESSAGE);			
			
			ServerCarController.this.clear();
			
			System.out.println("*=*   Car   *=*");
			System.out.println("Car Model: " + carModel);
			System.out.println("Type: " + type);
			System.out.println("License Plate: " + licensePlate);
			System.out.println("Uf: " + uf);
			System.out.println("City: " + city);
			System.out.println("Driver: " + driver);
			System.out.println("Image: " + image);
			System.out.println("Stolen: " + stolen);
			System.out.println("*=*   ------------------   *=*");
		}
	}
	
	class ListenerClear implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			ServerCarController.this.clear();
		}
	}	
}
